package com.zp.qualitypractice.list;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.zp.qualitypractice.R;

/**
 * Created by izp on 2017/4/23.
 */

public class CustomLoadingView extends View {
    private int mBubbleColor;
    private boolean mHasAnimation;
    private Paint mPaint;
    private CustomLoadingAnimationController mLoadingViewController ;


    public CustomLoadingView(Context context) {
        super(context);
        init(null,0);
    }

    public CustomLoadingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs,0);
    }

    public CustomLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs,defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CustomLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(attrs,defStyleAttr);
    }


    private void init(AttributeSet attributeSet,int defStyle){
        TypedArray typedArray = getContext().obtainStyledAttributes(attributeSet, R.styleable.CustomLoadingView);
        mBubbleColor = typedArray.getColor(R.styleable.CustomLoadingView_bubble_color, Color.RED);
        typedArray.recycle();
//        我就说为什么没有在CustomLoadingProgressPainter中发现paint的初始化，原来是在这里初始化的
        mPaint = new Paint();
        mPaint.setColor(mBubbleColor);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mLoadingViewController = new CustomLoadingProgressPainter();//这就是多态的应用，这样，他可以既能使用父的也能使用子的
        mLoadingViewController.setmTarget(this);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        超类仅仅是抛出一个异常，需要重写
        int width = measureCase(dp2px(30),widthMeasureSpec);
        int height = measureCase(dp2px(30),heightMeasureSpec);
        setMeasuredDimension(width,height);//确定测量值
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mLoadingViewController.draw(canvas,mPaint);//把画布和初始化好的画笔传给后面的操作者
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);//view应该不用重写onLayout啊？
        if (!mHasAnimation) {//如果已经初始化了，onLayout反腐调用的时候就不再调用
            mHasAnimation = true;
            mLoadingViewController.initAnimation();
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mLoadingViewController.setAnimationStatus(CustomLoadingAnimationController.AnimStatus.START);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mLoadingViewController.setAnimationStatus(CustomLoadingAnimationController.AnimStatus.CANCEL);
    }

    @Override
    public void setVisibility(int visibility) {
        if (getVisibility()==visibility){//如果想要设置的状态不是当前的visibility状态
            super.setVisibility(visibility);
            //已经隐藏或者显示了才调用动画？
            if (visibility==View.GONE || visibility==View.INVISIBLE){
                mLoadingViewController.setAnimationStatus(CustomLoadingAnimationController.AnimStatus.EDN);
            }else {
                mLoadingViewController.setAnimationStatus(CustomLoadingAnimationController.AnimStatus.START);
            }

        }

    }

    private int measureCase(int defSize, int measureSize){
        int resultSize = defSize;//30dp的默认值，换算成px，
        int specMode = MeasureSpec.getMode(measureSize);
        int specSize = MeasureSpec.getSize(measureSize);
        if (specMode == MeasureSpec.EXACTLY){
            resultSize = specSize;
        }else if(specMode == MeasureSpec.AT_MOST){
            resultSize = Math.min(defSize,specSize);
        }
        return resultSize;
    }
    private int dp2px(int dpValue) {
        return (int)(getContext().getResources().getDisplayMetrics().density*dpValue);
    }
}
